package org.mdrtwell.common;

import java.io.FileOutputStream;
import java.io.OutputStream;

import jxl.Workbook;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class JXLDemo {
	public static void main(String[] args) {
		JXLDemo.test();
	}

	public static void test() {
		String targetfile = "c:/out.xls";
		String worksheet = "List";
		String[] title = { "ID", "NAME", "DESCRIB" };
		WritableWorkbook workbook;
		try {
			System.out.println("begin");
			OutputStream os = new FileOutputStream(targetfile);
			workbook = Workbook.createWorkbook(os);
			WritableSheet sheet = workbook.createSheet(worksheet, 0);
			jxl.write.Label label;
			for (int i = 0; i < title.length; i++) {
				label = new jxl.write.Label(i, 0, title[i]); // put the title
				// in row 0
				sheet.addCell(label);
			}
			jxl.write.Number number = new jxl.write.Number(3, 4, 3.14159);
			sheet.addCell(number);
			jxl.write.WritableFont font = new jxl.write.WritableFont(WritableFont.TIMES,
					10,
					WritableFont.BOLD,
					true);
			jxl.write.WritableCellFormat format = new jxl.write.WritableCellFormat(font);
			jxl.write.Label labelCF = new jxl.write.Label(4, 4, "�ı�", format);
			sheet.addCell(labelCF);
			jxl.write.WritableFont font2 = new jxl.write.WritableFont(WritableFont.ARIAL,
					10,
					WritableFont.BOLD,
					false,
					jxl.format.UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.RED);
			jxl.write.WritableCellFormat format2 = new jxl.write.WritableCellFormat(font2);
			format2.setBackground(jxl.format.Colour.BLUE);
			jxl.write.Label labelCFC = new jxl.write.Label(1, 5, "����ɫ", format2);
			sheet.addCell(labelCFC);
			jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
			jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
			jxl.write.Number labelNF = new jxl.write.Number(1, 1, 3.1415926, wcfN);
			sheet.addCell(labelNF);
			jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false);
			sheet.addCell(labelB);
			jxl.write.DateTime labelDT = new jxl.write.DateTime(0, 3, new java.util.Date());
			sheet.addCell(labelDT);
			jxl.write.DateFormat df = new jxl.write.DateFormat("ddMMyyyyhh:mm:ss");
			jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
			jxl.write.DateTime labelDTF = new jxl.write.DateTime(1, 3, new java.util.Date(), wcfDF);
			sheet.addCell(labelDTF);
			sheet.mergeCells(4, 5, 8, 10);// ���Ͻǵ����½�
			jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,
					40,
					WritableFont.BOLD,
					false,
					jxl.format.UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.GREEN);
			jxl.write.WritableCellFormat wchB = new jxl.write.WritableCellFormat(wfc);
			wchB.setAlignment(jxl.format.Alignment.CENTRE);
			labelCFC = new jxl.write.Label(4, 5, "��Ԫ�ϲ�", wchB);
			sheet.addCell(labelCFC); //
			jxl.write.WritableCellFormat wcsB = new jxl.write.WritableCellFormat();
			wcsB.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THICK);
			labelCFC = new jxl.write.Label(0, 6, "�߿�����", wcsB);
			sheet.addCell(labelCFC);
			workbook.write();
			workbook.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("end");
		Runtime r = Runtime.getRuntime();
		Process p = null;
		String cmd[] = { "C:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE", "out.xls" };
		try {
			p = r.exec(cmd);
		} catch (Exception e) {
			System.out.println("error executing: " + cmd[0]);
		}
	}
}
